package com.system.borrow.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.system.borrow.R;
import com.system.borrow.bean.GloData;

import java.lang.reflect.Field;

/**
 * 作者：dyj
 * 日期：2016/11/25 17:15
 * 说明：
 */
public class ChooseCity implements View.OnClickListener, NumberPicker.OnValueChangeListener {
    private Activity mContext;
    private Dialog dialog;
    private ChooseCityInterface cityInterface;
    private NumberPicker npProvince, npCity;
    private TextView tvCancel, tvSure;
    private String[] newCityArray = new String[2];
    private String[] newCodeArray = new String[2];
//    private Province bean;

    public void createDialog(Activity context, String[] oldCityArray,String[] oldCodeArray, ChooseCityInterface cityInterface) {
        this.mContext = context;
        this.cityInterface = cityInterface;

        // 使用fastjson,用户可自行选择添加依赖和jar包
        // bean = JSON.parseObject(CityData.getJson(), CityBean.class);

//        Gson gson = new Gson();
//        bean = gson.fromJson(CityData.getJson(), CityBean.class);

        newCityArray[0] = oldCityArray[0];
        newCityArray[1] = oldCityArray[1];
        newCodeArray[0] = oldCodeArray[0];
        newCodeArray[1] = oldCodeArray[1];
//        newCityArray[2] = oldCityArray[2];

        dialog = new Dialog(context, R.style.mask_dialog);
        dialog.setCanceledOnTouchOutside(true);
        View dialogView = View.inflate(context, R.layout.dialog_choose_city, null);
        dialog.setContentView(dialogView, new RelativeLayout.LayoutParams(
                context.getWindowManager().getDefaultDisplay().getWidth(), WindowManager.LayoutParams.MATCH_PARENT));
        //初始化控件
        tvCancel = (TextView) dialogView.findViewById(R.id.tvCancel);
        tvSure = (TextView) dialogView.findViewById(R.id.tvSure);
        npProvince = (NumberPicker) dialogView.findViewById(R.id.npProvince);
        npCity = (NumberPicker) dialogView.findViewById(R.id.npCity);
        dialog.setFeatureDrawableAlpha(Window.FEATURE_OPTIONS_PANEL, 0);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
        dialogWindow.setWindowAnimations(R.style.popwin_anim_style);
        dialog.show();


        tvCancel.setOnClickListener(this);
        tvSure.setOnClickListener(this);
//        npCounty = (NumberPicker) window.findViewById(R.id.npCounty);
        setNomal();
        //省：设置选择器最小值、最大值、初始值
        String[] provinceArray = new String[GloData.getList().size()];//初始化省数组
//        String[] provinceCodeArray = new String[GloData.getList().size()];
        for (int i = 0; i < provinceArray.length; i++) {//省数组填充数据
//            provinceArray[i] = bean.getData().get(i).getName();
            provinceArray[i] = GloData.getList().get(i).name;
//            provinceCodeArray[i] = GloData.getList().get(i).value;
        }

        npProvince.setDisplayedValues(provinceArray);//设置选择器数据、默认值
        npProvince.setMinValue(0);
        npProvince.setMaxValue(provinceArray.length - 1);
        for (int i = 0; i < provinceArray.length; i++) {
            if (provinceArray[i].equals(newCityArray[0])) {
                npProvince.setValue(i);
                changeCity(i);//联动市数据
            } else {
                if (GloData.getCustomerDTO().getCityId() == 0) {
                    newCityArray[0] = GloData.getList().get(npProvince.getValue()).name;
                    newCodeArray[0] = GloData.getList().get(npProvince.getValue()).value;
                    changeCity(npProvince.getValue());
                    newCityArray[1] = GloData.getList().get(npProvince.getValue()).child.get(0).name;
                    newCodeArray[1] = GloData.getList().get(npProvince.getValue()).child.get(0).value;
                } else {

                }
            }
        }

    }

    //根据省,联动市数据
    private void changeCity(int provinceTag) {
//        List<CityBean.Data.City> cityList = bean.getData().get(provinceTag).getCity();
        String[] cityArray = new String[GloData.getList().get(provinceTag).child.size()];
        for (int i = 0; i < cityArray.length; i++) {
//            cityArray[i] = cityList.get(i).getName();
            cityArray[i] = GloData.getList().get(provinceTag).child.get(i).name;
        }

//        LogUtil.i(Arrays.toString(cityArray));
        try {
            npCity.setMinValue(0);
            npCity.setMaxValue(cityArray.length - 1);
            npCity.setWrapSelectorWheel(false);
            npCity.setDisplayedValues(cityArray);//设置选择器数据、默认值
        } catch (Exception e) {
            npCity.setDisplayedValues(cityArray);//设置选择器数据、默认值
            npCity.setMinValue(0);
            npCity.setMaxValue(cityArray.length - 1);
            npCity.setWrapSelectorWheel(false);
        }
//        for (int i = 0; i < cityArray.length; i++) {
//            if (cityArray[i].equals(newCityArray[1])) {
//                npCity.setValue(i);
//               changeCounty(provinceTag, i);//联动县数据
//                return;
//            }
//        }
        for (int i = 0; i < cityArray.length; i++) {
            if (newCityArray[1].equals(cityArray[i])) {
                npCity.setValue(i);
                break;
            }
        }
//        changeCounty(provinceTag, npCity.getValue());//联动县数据
    }

    //根据市,联动县数据
//    private void changeCounty(int provinceTag, int cityTag) {
//        List<String> countyList = bean.getData().get(provinceTag).getCity().get(cityTag).getCounty();
//        String[] countyArray = new String[countyList.size()];
//        for (int i = 0; i < countyArray.length; i++) {
//            countyArray[i] = countyList.get(i).toString();
//        }
//        LogUtil.i(Arrays.toString(countyArray) + countyArray.length + " - " + (countyArray.length - 1));
//        try {
//            npCounty.setMinValue(0);
//            npCounty.setMaxValue(countyArray.length - 1);
//            npCounty.setWrapSelectorWheel(false);
//            npCounty.setDisplayedValues(countyArray);//设置选择器数据、默认值
//        } catch (Exception e) {
//            npCounty.setDisplayedValues(countyArray);//设置选择器数据、默认值
//            npCounty.setMinValue(0);
//            npCounty.setMaxValue(countyArray.length - 1);
//            npCounty.setWrapSelectorWheel(false);
//        }
//        for (int i = 0; i < countyArray.length; i++) {
//            if (countyArray[i].equals(newCityArray[2])) {
//                npCounty.setValue(i);
//                return;
//            }
//        }
//        npCounty.setValue(0);
//    }

    //设置NumberPicker的分割线透明、字体颜色、设置监听
    private void setNomal() {
        //设置监听
        npProvince.setOnValueChangedListener(this);
        npCity.setOnValueChangedListener(this);
//        npCounty.setOnValueChangedListener(this);
        //去除分割线
        setNumberPickerDividerColor(npProvince);
        setNumberPickerDividerColor(npCity);
//        setNumberPickerDividerColor(npCounty);
        //设置字体颜色
        setNumberPickerTextColor(npProvince, mContext.getResources().getColor(R.color.black));
        setNumberPickerTextColor(npCity, mContext.getResources().getColor(R.color.black));
//        setNumberPickerTextColor(npCounty, mContext.getResources().getColor(R.color.black));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCancel:
                dialog.dismiss();
                break;
            case R.id.tvSure:
                dialog.dismiss();
                if (newCityArray != null && newCodeArray != null) {
                    cityInterface.sure(newCityArray, newCodeArray);
                }
                break;
        }
    }

    //选择器选择值监听
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.npProvince:
//                List<CityBean.Data> dataList = bean.getData();
//                newCityArray[0] = dataList.get(npProvince.getValue()).getName();
                newCityArray[0] = GloData.getList().get(npProvince.getValue()).name;
                newCodeArray[0] = GloData.getList().get(npProvince.getValue()).value;
                changeCity(npProvince.getValue());
//                newCityArray[1] = dataList.get(npProvince.getValue()).getCity().get(0).getName();
//                newCityArray[2] = dataList.get(npProvince.getValue()).getCity().get(0).getCounty().get(0).toString();
                newCityArray[1] = GloData.getList().get(npProvince.getValue()).child.get(0).name;
                newCodeArray[1] = GloData.getList().get(npProvince.getValue()).child.get(0).value;
//                newCityArray[2] = dataList.get(npProvince.getValue()).getCity().get(0).getCounty().get(0).toString();
                break;
            case R.id.npCity:
//                List<CityBean.Data.City> cityList = bean.getData().get(npProvince.getValue()).getCity();
//                newCityArray[1] = cityList.get(npCity.getValue()).getName();
                newCityArray[1] = GloData.getList().get(npProvince.getValue()).child.get(npCity.getValue()).name;
                newCodeArray[1] = GloData.getList().get(npProvince.getValue()).child.get(npCity.getValue()).value;
//                changeCounty(npProvince.getValue(), npCity.getValue());
//                newCityArray[2] = cityList.get(npCity.getValue()).getCounty().get(0).toString();
                break;
//            case R.id.npCounty:
//                List<String> countyList = bean.getData().get(npProvince.getValue()).getCity().get(npCity.getValue()).getCounty();
//                newCityArray[2] = countyList.get(npCounty.getValue()).toString();
//                break;
        }
    }

    //设置分割线颜色
    private void setNumberPickerDividerColor(NumberPicker numberPicker) {
        NumberPicker picker = numberPicker;
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(picker, new ColorDrawable(mContext.getResources().getColor(R.color.home_text_select)));// pf.set(picker, new Div)
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    //设置选择器字体颜色
    private static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
        boolean result = false;
        final int count = numberPicker.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = numberPicker.getChildAt(i);
            if (child instanceof EditText) {
                try {
                    Field selectorWheelPaintField = numberPicker.getClass()
                            .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
                    ((EditText) child).setTextColor(color);
                    numberPicker.invalidate();
                    result = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
